package cn.itsource.user.service.impl;

import cn.itsource.basic.contants.SystemContants;
import cn.itsource.basic.service.impl.BasicServiceImpl;
import cn.itsource.basic.util.MD5Utils;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.mapper.EmployeeMapper;
import cn.itsource.org.mapper.ShopMapper;
import cn.itsource.org.service.IShopService;
import cn.itsource.user.domain.LoginInfo;
import cn.itsource.user.domain.User;
import cn.itsource.user.mapper.LoginInfoMapper;
import cn.itsource.user.mapper.UserMapper;
import cn.itsource.user.service.IUserService;
import cn.itsource.user.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.UUID;

@Service
public class UserServiceImpl extends BasicServiceImpl<User> implements IUserService{

    @Autowired
    private LoginInfoMapper loginInfoMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private UserMapper userMapper;

    @Transactional
    @Override
    public void register(UserVO userVO) {
        //细节1 验证数据
        if (StringUtils.isEmpty(userVO.getPhone()) || StringUtils.isEmpty(userVO.getCode()) ||
                StringUtils.isEmpty(userVO.getPassword()) || StringUtils.isEmpty(userVO.getPasswordRepeat())) {
            throw  new RuntimeException("请完善数据");
        }
        //验证两次密码是否一致
        if (!userVO.getPassword().equals(userVO.getPasswordRepeat())){
            throw  new RuntimeException("两次密码不一致");
        }
        // 2.是否有注册验证
        User reguser = userMapper.loadByPhone(userVO.getPhone());
        if (reguser != null) {
            throw new RuntimeException("该手机已经注册，请直接登录..");
        }
        //验证code
        //从redis里面获取给用户发送的验证码
        Object oCode = redisTemplate.opsForValue().get(userVO.getPhone() + ":" + SystemContants.REGISTER);
        if (oCode==null){
            throw  new RuntimeException("验证码已失效，请重新获取");
        }else {
            String code =  (String)oCode;
            //取出redis里面的验证码
            String redisCode = code.split(":")[0];
            if (!redisCode.equals(userVO.getCode())){
                throw  new RuntimeException("验证码错误，请重新输入");
            }
        }


        //把UserVO转换为User
        User user = userVoToUser(userVO);
        LoginInfo loginInfo = userToLoginInfo(user);
        //1.t_logininfo 保存一条数据   返回主键ID
        loginInfoMapper.save(loginInfo);
        //维护关联字段
        user.setLogininfo_id(loginInfo.getId());
        //2.在t_user保存一条数据
        super.add(user);

    }

    @Override
    public User loadByLoginId(Long id) {
        return userMapper.loadByLoginId(id);
    }

    private LoginInfo userToLoginInfo(User user){
        LoginInfo loginInfo = new LoginInfo();
        //属性拷贝 --》javaBean
        BeanUtils.copyProperties(user, loginInfo);
        //处理type属性
        loginInfo.setType(1);
        loginInfo.setDisable(user.getState());
        return  loginInfo;
    }


    private User userVoToUser(UserVO userVO){
        User user = new User();
        user.setPhone(userVO.getPhone());
        //细节2 密码的加密
        //生成随机的盐
        UUID salt = UUID.randomUUID();
        user.setSalt(salt.toString());
        //将密码加密过后进行存储
        user.setPassword(MD5Utils.encrypByMd5(userVO.getPassword()+salt));
        //手机注册默认就是启用状态
        user.setState(1);
        return  user;
    }
}
